import { Helper } from 'babylon-lib/tool/helper';
import { DefaultStage } from '../../default-stage';
import { Model } from '../model';
import { RawAssetsName } from '../../../raw-assets-info';
import { InstancedMesh } from '@babylonjs/core/Meshes/instancedMesh';
import { Mesh } from '@babylonjs/core/Meshes/mesh';
import { Color3 } from '@babylonjs/core/Maths/math.color';
import { Scene } from '@babylonjs/core/scene';
import { GradientSkybox } from 'babylon-lib/preset/gradient-skybox';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { Random } from 'babylon-lib/tool/random';
import { AbstractMesh, AnimationFrame } from 'babylon-lib/index';
import * as Data from '../../../data/data';
import { PBRMaterial } from '@babylonjs/core';
import { AbstractLandscape } from './abstract-landscape';


class Landscape extends AbstractLandscape {

    public static readonly enemyTargetDirection = new Vector3(1, 0, 3).normalize();
    public static readonly refCoordinate = new Vector3(5523418, 0, 7261433);

    public static initialize(stage: DefaultStage): void {
        super.initialize(stage);
        Data.windStrength.value = this.wind.length();
    }

    public static checkDamage(): void {
    }

    protected static setSky(stage: DefaultStage): void {
        const scn = stage.mainScene;
        scn.fogEnabled = true;
        scn.fogStart = 100;
        scn.fogEnd = 6000;
        scn.fogColor = Color3.FromHexString('#292929');
        scn.fogMode = Scene.FOGMODE_LINEAR;

        const skybox = new GradientSkybox('sky', stage.mainScene, {
            values: [0.2, 0.5, 0.8],
            colors: ['#222222', '#888888', '#000000'],
            size: 1000,
        });

        skybox.mesh.scaling.set(12, 3, 12);
    }

    protected static setModel(stage: DefaultStage): void {
        const all = Model.dixing;
        const meshes = Helper.getChildMeshes(all);
        meshes.forEach(m => {
            (m.material as PBRMaterial).disableLighting = true;
        });
        const ground = Helper.nodeInArray(RawAssetsName.groundMesh, meshes, undefined, true) as Mesh;
        const _trees: AbstractMesh[] = [];
        Helper.nodeInArray(RawAssetsName.treeMesh, meshes, _trees, false) as Mesh;
        const tree = _trees[0] as Mesh;
        const grounds: Array<InstancedMesh> = [];
        const trees: Array<InstancedMesh> = [];
        for (let i = 0; i < 8; i++) {
            grounds.push(ground.createInstance(RawAssetsName.groundMesh + i));
            trees.push(tree.createInstance(RawAssetsName.treeMesh + i));
        }
        const extent = 4000;

        grounds[0].position.set(extent, 0, 0);
        trees[0].position.set(extent, 0, 0);

        grounds[1].position.set(-extent, 0, 0);
        trees[1].position.set(-extent, 0, 0);

        grounds[2].position.set(-extent, 0, extent);
        trees[2].position.set(-extent, 0, extent);

        grounds[3].position.set(extent, 0, -extent);
        trees[3].position.set(extent, 0, -extent);

        grounds[4].position.set(extent, 0, extent);
        trees[4].position.set(extent, 0, extent);

        grounds[5].position.set(-extent, 0, -extent);
        trees[5].position.set(-extent, 0, -extent);

        grounds[6].position.set(0, 0, extent);
        trees[6].position.set(0, 0, extent);

        grounds[7].position.set(0, 0, -extent);
        trees[7].position.set(0, 0, -extent);
    }

}


export { Landscape };